local skel = fk.CreateSkill {
  name = "lb_zero__bainiang",
  attached_skill_name = "lb_zero__bainiang_active&",
  max_branches_use_time = {
    ["amazing_grace"] = { [Player.HistoryTurn] = 1 }
  }
}

Fk:loadTranslationTable {
  ["lb_zero__bainiang"] = "百酿",
  [":lb_zero__bainiang"] = "每回合限一次，你可以将一张牌当【五谷丰登】使用，所有角色可以将获得牌当【酒】使用。",

  ["#lb_zero__bainiang"] = "百酿：你可以将一张牌当【五谷丰登】使用，所有角色可以将获得牌当【酒】使用",

  ["@@lb_zero__bainiang-inarea"] = "百酿",

  ["$lb_zero__bainiang1"] = "独家配方，量大管饱。",
  ["$lb_zero__bainiang2"] = "时机已到，大火收汁！",
  ["$lb_zero__bainiang3"] = "人是铁饭是钢，师兄我会溜肥肠。",
}

local getBaiNiang = function (player)
    local list = {}
    if skel:withinBranchTimesLimit(player, "amazing_grace", Player.HistoryTurn) then
      table.insert(list, "amazing_grace")
    end
    if table.find(player:getCardIds("he"), function(id)
          return Fk:getCardById(id):getMark("@@lb_zero__bainiang-inarea") ~= 0
        end) then
      table.insert(list, "analeptic")
    end
    return list
end

skel:addEffect("viewas", {
  mute = true,
  anim_type = "support",
  prompt = function (self, player, selected_cards, selected)
    local list = getBaiNiang(player)
    if #list > 0 then
      local name = self.interaction.data or getBaiNiang(player)[1]
      if name == "analeptic" then
        return "#lb_zero__bainiang_active"
      end
    end
    return "#lb_zero__bainiang"
  end,
  pattern = "analeptic,amazing_grace",
  handly_pile = true,
  interaction = function(self, player)
    local list = getBaiNiang(player)
    if #list == 2 then
      return UI.ComboBox { choices = list }
    end
  end,
  card_filter = function(self, player, to_select, selected, selected_targets)
    if #selected == 0 and #getBaiNiang(player) > 0 then
      local name = self.interaction.data or getBaiNiang(player)[1]
      if name == "analeptic" then
        return Fk:getCardById(to_select):getMark("@@lb_zero__bainiang-inarea") ~= 0
      else
        return true
      end
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or #getBaiNiang(player) == 0 then return nil end
    local name = self.interaction.data or getBaiNiang(player)[1]
    local card = Fk:cloneCard(name)
    card:addSubcards(cards)
    card.skillName = skel.name
    if name == "amazing_grace" then
      card.skill = Fk.skills["lb_zero_amazing_grace_skill"]
    end
    return card
  end,
  before_use = function(self, player, use)
    local audio = math.random(1, 2)
    if use.card.name == "amazing_grace" then
      player:addSkillBranchUseHistory(skel.name, "amazing_grace")
      use.extra_data = use.extra_data or {}
      use.extra_data.lb_zero__bainiang = true
    else
      audio = audio + 1
    end
    player.room:notifySkillInvoked(player, skel.name, "support")
    player:broadcastSkillInvoke(skel.name, audio)
  end,
  enabled_at_play = function(self, player)
    return #getBaiNiang(player) > 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and #getBaiNiang(player) > 0
  end
})

return skel
